﻿<!DOCTYPE html>
@{
    string selected = Request.QueryString["selected"];
    string from = Request.QueryString["from"];
    
    string selectQueryString = "";
   
    string connString =  System.Configuration.ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
    MySql.Data.MySqlClient.MySqlConnection connection = new MySql.Data.MySqlClient.MySqlConnection(connString);

    switch(from)
    {
        case "hosts":
        {
            selectQueryString = "DELETE FROM "+from+" WHERE name='"+selected+"';";
            break;
        }
        case "subnets":
        {
            MySql.Data.MySqlClient.MySqlCommand query = new MySql.Data.MySqlClient.MySqlCommand("SELECT id FROM subnets WHERE name='"+selected+"';", connection);
            connection.Open();
            MySql.Data.MySqlClient.MySqlDataReader reader = query.ExecuteReader();
            
            reader.Read();
            System.Data.IDataRecord record = (System.Data.IDataRecord)reader;

            selectQueryString = "DELETE FROM hosts WHERE subnet="+record.GetString(0)+";"+
            "DELETE FROM "+from+" WHERE name='"+selected+"';";

            reader.Close();
            connection.Close();
            break;
        }
        case "servers":
        {
            // TO DO
            // Usuwanie wybranego serwera
            try
            {
                // Pobieram id serwera
                MySql.Data.MySqlClient.MySqlCommand query = new MySql.Data.MySqlClient.MySqlCommand("SELECT id FROM servers WHERE name='"+selected+"';", connection);
                connection.Open();
                int id = Convert.ToInt32(query.ExecuteScalar());
                connection.Close();


                // Pobieram wszystkie id podsieci
                query = new MySql.Data.MySqlClient.MySqlCommand("SELECT id FROM subnets WHERE server='"+id+"';", connection);
                connection.Open();
                MySql.Data.MySqlClient.MySqlDataReader reader = query.ExecuteReader();

                List<String> subnets_list = new List<String>();

                while(reader.Read())
                {
                    System.Data.IDataRecord record = (System.Data.IDataRecord)reader;

                    subnets_list.Add(record.GetString(0));
                }
                reader.Close();
                connection.Close();

                // Usuwam hosty
                foreach(string item in subnets_list)
                {
                    selectQueryString += "DELETE FROM hosts WHERE subnet="+item+";";
                }
                // Usuwam podsieci
                foreach(string item in subnets_list)
                {
                    selectQueryString += "DELETE FROM subnets WHERE id="+item+";";
                }
                // Usuwam serwer
                selectQueryString += "DELETE FROM servers WHERE id="+id+";";
            }
            catch(Exception ex)
            {
                <html lang="en">
                    <head>
                        <meta charset="utf-8" />
                        <title>Layout</title>
                        <link href="Images/favicon.ico" rel="shortcut icon" type="image/x-icon" />
                        <link rel="stylesheet" type="text/css" href="Shared/styles.css">
                    </head>
                    <body>
                        <div style="background-color: #fff; width: 500px; height: auto; margin-left: auto; margin-right: auto; margin-top: 400px">
                            <p style="color: #f00; margin-left: 10px; margin-right: 10px; padding-top: 10px; font-family: 'Segoe UI'; font-size: medium; text-align: justify"><strong>Błąd!</strong></p>

                            <p style="margin-left: 10px; margin-right: 10px; font-family: 'Segoe UI'; font-size: medium; text-align: justify">@ex.Message}</p>
                            @{
                                switch(from)
                                {
                                    case "servers":
                                    {
                                        <button onclick="navigate('default.cshtml')" style="width: 100px; margin-left: 200px; margin-bottom: 10px; margin-top: 20px; position: relative">OK</button>
                                        break;
                                    }
                                    case "hosts":
                                    {
                                        <button onclick="navigate('Hosts.cshtml')" style="width: 100px; margin-left: 200px; margin-bottom: 10px; margin-top: 20px; position: relative">OK</button>
                                        break;
                                    }
                                    case "subnets":
                                    {
                                        <button onclick="navigate('Subnets.cshtml')" style="width: 100px; margin-left: 200px; margin-bottom: 10px; margin-top: 20px; position: relative">OK</button>
                                        break;
                                    }
                                }
                            }
                        </div>
                    </body>
                </html>
            }
            break;
        }
    }

    // Wykonuję przygotowany selectQueryString
    try
    {
        connection.Open();
        MySql.Data.MySqlClient.MySqlCommand command = new MySql.Data.MySqlClient.MySqlCommand(selectQueryString, connection);
        command.ExecuteNonQuery();

        // Powrót do podstrony
        switch(from)
        {
            case "hosts":
            {
                Response.Redirect("hosts.cshtml");
                break;
            }
            case "subnets":
            {
                Response.Redirect("subnets.cshtml");
                break;
            }
            case "servers":
            {
                Response.Redirect("default.cshtml");
                break;
            }
        }
    }
    catch(Exception ex)
    {
        <html lang="en">
            <head>
                <meta charset="utf-8" />
                <title>Layout</title>
                <link href="Images/favicon.ico" rel="shortcut icon" type="image/x-icon" />
                <link rel="stylesheet" type="text/css" href="Shared/styles.css">
            </head>
            <body>
                <div style="background-color: #fff; width: 500px; height: auto; margin-left: auto; margin-right: auto; margin-top: 400px">
                    <p style="color: #f00; margin-left: 10px; margin-right: 10px; padding-top: 10px; font-family: 'Segoe UI'; font-size: medium; text-align: justify"><strong>Błąd!</strong></p>

                    <p style="margin-left: 10px; margin-right: 10px; font-family: 'Segoe UI'; font-size: medium; text-align: justify">@selectQueryString</p>
                    @{
                        switch(from)
                        {
                            case "servers":
                            {
                                <button onclick="navigate('default.cshtml')" style="width: 100px; margin-left: 200px; margin-bottom: 10px; margin-top: 20px; position: relative">OK</button>
                                break;
                            }
                            case "hosts":
                            {
                                <button onclick="navigate('Hosts.cshtml')" style="width: 100px; margin-left: 200px; margin-bottom: 10px; margin-top: 20px; position: relative">OK</button>
                                break;
                            }
                            case "subnets":
                            {
                                <button onclick="navigate('Subnets.cshtml')" style="width: 100px; margin-left: 200px; margin-bottom: 10px; margin-top: 20px; position: relative">OK</button>
                                break;
                            }
                        }
                    }
                </div>
            </body>
        </html>
    }
    finally
    {
        connection.Close();
    }
}